//
//  BoxDownLoadViewController.m
//  WuliHanhan
//
//  Created by tang on 16/3/9.
//  Copyright © 2016年 LVpeng. All rights reserved.
//

#import "BoxDownLoadViewController.h"
#import "Header.h"
#import "DataBaseBoxImageModel.h"
#import "DataBaseSaveEditeImageModel.h"


#import "ImagePushButton.h"
#import "LOadImageView.h"
//#import "ImagePushButton.h"


#import "DownLoadImageCollectionViewCell.h"
#import "BaseCreatViewController.h"

@interface BoxDownLoadViewController ()<UICollectionViewDataSource, UICollectionViewDelegateFlowLayout,ImagePushButtonDelegate, DownLoadImageCollectionViewCellDelegate>
{
    ImagePushButton *imagePushButton;
    NSMutableArray *imageMutableArray;
    NSMutableArray *connectIDMutableArray;
    
    NSMutableArray *selectMutableArray;
    
    LOadImageView *loadIMageView;
    
    
    int totleInt;
    int lastInt;
}

@property (strong, nonatomic)UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *selectIDarray;

@end

@implementation BoxDownLoadViewController

- (void)loadView
{
    [super loadView];
    
    imageMutableArray = [NSMutableArray new];
    selectMutableArray = [NSMutableArray new];
    connectIDMutableArray = [NSMutableArray new];
    
    _selectIDarray = [NSMutableArray new];
    
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [self.navigationController setNavigationBarHidden:YES];
    
    UIView *titleView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, WithView, 44.0)];
    [titleView setBackgroundColor:uicolor(254, 195, 9)];
    [self.view addSubview:titleView];
    
    UILabel *titleLable = [[UILabel alloc] initWithFrame:CGRectMake(0, 12.5, WithView, 20.0)];
    [titleLable setTextAlignment:NSTextAlignmentCenter];
    [self.view addSubview:titleLable];
    [titleLable setText:@"我的收藏"];
    [titleLable setTextColor:uicolor(13, 20, 25)];

    
    
    UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc] init];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    
    self.collectionView=[[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, 320, heightView-64) collectionViewLayout:flowLayout];
    self.collectionView.dataSource=self;
    self.collectionView.delegate=self;
    [self.collectionView setBackgroundColor:[UIColor clearColor]];
    
    //注册Cell，必须要有
    [self.collectionView registerClass:[DownLoadImageCollectionViewCell class] forCellWithReuseIdentifier:@"UICollectionViewCell"];
    
   
    
    
    UIImage *image = nil;
    
    image = [UIImage imageNamed:@"quxiao.png"];
    
    UIButton *calcanButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
    [calcanButton setBackgroundImage:image forState:UIControlStateNormal];
    [self.view addSubview:calcanButton];
    [calcanButton addTarget:self action:@selector(cancleToPopView) forControlEvents:UIControlEventTouchUpInside];
    
    imagePushButton = [[ImagePushButton alloc] initWithFrame:CGRectMake(0, 0, 64, 26)];
    [imagePushButton setCenter:CGPointMake(WithView-42.0, 22.0)];
    [imagePushButton setMImagePushButtonDelegate:self];
    [self.view addSubview:imagePushButton];

    
    image = [UIImage imageNamed:@"NoData.png"];
    UIImageView *nodataImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 100, image.size.width, image.size.height)];
    [nodataImageView setCenter:CGPointMake(WithView/2.0, 310.0)];
    [nodataImageView setImage:image];
    [self.view addSubview:nodataImageView];
    
    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(nodataImageView.frame)+14.0, WithView, 20.0)];
    [lable setTextAlignment:NSTextAlignmentCenter];
    [lable setTextColor:uicolor(34, 34, 34)];
    [self.view addSubview:lable];
    [lable setText:@"暂无收藏任何素材"];

     [self.view addSubview:self.collectionView];
    
    loadIMageView = [[LOadImageView alloc] initWithFrame:CGRectMake(0, 0, WithView, WithView)];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [_collectionView setBackgroundColor:uicolor(237, 237, 237)];
    
    if (_plusImage)
    {
        //        [nameIndexArray addObjectsFromArray:_nameIndexArray];
        //        [yasuotuArray addObjectsFromArray:_imageYasuoArray];
    }
    else
    {
        //第一次进入
        [[DataBaseBoxImageModel sharedInstance] openDataBase];
    }

    [self selectBoxImage];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(jiantingshumu:) name:@"shumu" object:nil];
}



- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}



//选择照片数目
- (void)jiantingshumu:(NSNotification *)notion
{
    totleInt = [[notion object] intValue];
//    [imagePushButton    setNumberInt:totleInt + lastInt];
}



- (void)selectBoxImage
{
    [imageMutableArray removeAllObjects];
    [connectIDMutableArray removeAllObjects];
    
    
    
    [[DataBaseBoxImageModel sharedInstance] readDownLoadImage:^(NSArray *connectIDArray, NSArray *dataArray){
    
        if ([connectIDArray count] == 0)
        {
            [_collectionView setHidden:YES];
        }
        else
        {
            [_collectionView setHidden:NO];
            [connectIDMutableArray addObjectsFromArray:connectIDArray];
            [imageMutableArray addObjectsFromArray:dataArray];
            [_collectionView reloadData];

        }
    }];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



//返回
- (void)cancleToPopView
{
    [self dismissViewControllerAnimated:YES completion:nil];
//    [[UIApplication sharedApplication] setStatusBarHidden:NO];
}



//进入创作
- (void)pushToCreatView
{
    
//    if (_plusImage)
//    {
//        [self dismissViewControllerAnimated:YES completion:nil];
//        
//    }
//    else
//    {
////        //        BaseCreatViewController *creatImageVC = [[BaseCreatViewController alloc] init];
////        
//        if (totleInt == 0)
//        {
////
//        }
//        else
//        {
//        [self.navigationController pushViewController:[BaseCreatViewController sharedInstance] animated:YES];
//        
//        }
//
//    }
    [_MBoxDownLoadViewControllerDelegate BOXPush];
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return imageMutableArray.count;
}


//每个UICollectionView展示的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * CellIdentifier = @"UICollectionViewCell";
    DownLoadImageCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
    [cell setTag:indexPath.row];
    [cell setDataImage:[UIImage imageWithData:imageMutableArray[indexPath.row]]];
    [cell setMDownLoadImageCollectionViewCellDelegate:self];
    [cell setModelWith:@[]];
    return cell;
}

#pragma mark --UICollectionViewDelegateFlowLayout

//定义每个Item 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(biliwith(77.0), biliwith(77.0));
}

//定义每个UICollectionView 的 margin
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}



- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    
    return 4.0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 4.0;
}



- (void)chooseImageWithIndex:(NSInteger)index withSelectBool:(BOOL)select
{
    if (select)
    {
        [_selectIDarray addObject:[connectIDMutableArray objectAtIndex:index]];
        [selectMutableArray addObject:[imageMutableArray objectAtIndex:index]];
    
    }
    else
    {
        [selectMutableArray removeObject:[imageMutableArray objectAtIndex:index]];
        [_selectIDarray removeObject:[connectIDMutableArray objectAtIndex:index]];
    }
    [imagePushButton setNumberInt:(int)selectMutableArray.count];
    
    [_MBoxDownLoadViewControllerDelegate withSelectPhoto:selectMutableArray withIDarray:_selectIDarray];
}














/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
